<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Comment_Booklet extends Controller{

private $max_comment_length = 140;

public function action_index()
{
}

public function action_list()
{
	$booklet_comment = ORM::factory('comment_booklet');
	if ($_GET)
	{
		$booklet_id = arr::get($_GET, 'booklet_id');
		if ($booklet_id)
		{
			$booklet_comment->where('booklet_id', '=', $booklet_id);
		}
	}
	$query_result = $booklet_comment->find_all();
	$result = $this->create_booklet_comment_list($query_result->as_array());
	json_Output::write($result);
}

public function action_create()
{
	return;
	//$booklet_id = arr::get($_POST, 'booklet_id');
	//$text = arr::get($_POST, 'text');
	$booklet_id = arr::get($_GET, 'booklet_id');
	$text = arr::get($_GET, 'text');

	if ($booklet_id == NULL)
	{
		header("Status: 400 对象id为空");
		return;
	}
	if ($text == NULL)
	{
		header("Status: 400 评论为空");
		return;
	}
	if (strlen($text) > $this->max_comment_length)
	{
		header("Status: 400 字数超出140个字");
		return;
	}
	$booklet = ORM::factory('booklet')->where('id', '=', $booklet_id)->find();
	if ( !$booklet->loaded() )
	{
		header("Status: 404 该书不存在");
		return;
	}
	$booklet_comment = ORM::factory('comment_booklet');
	$booklet_comment->values(array(
			'booklet_id'  => $booklet_id,
			'content' 	  => $text,
			'create_time' => time()
		));
	$booklet_comment->save();
}

protected function create_booklet_comment_list($array)
{
	$object = array();
	foreach ( $array as $booklet_comment)
	{
		array_push($object,
			array(
				'id'		   => $booklet_comment->id,
				'content'	   => $booklet_comment->content,
				'ip'   		   => $booklet_comment->ip,
				'create_time'  => $booklet_comment->create_time
			));
	}
	return array(
		'count' => count($object),
		'data'  => $object
	);
}



}



